package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Dish;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto){

        int row = dishService.add(dishDto);

        if(row >0 ){
            return R.success("添加菜品成功！");
        }else{
            return R.success("添加菜品失败！");
        }
    }


    /**
     * 分页查询菜品数据
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){

        try {
            //1. 调用service
            IPage<DishDto> page = dishService.findPage(pageParam);

            //2. 响应
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();

            return R.error("分页查询失败！");
        }
    }

    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        int row = dishService.update(dishDto);

        if(row >0 ){
            return R.success("更新菜品成功！");
        }else{
            return R.success("更新菜品失败！");
        }
    }

    /**
     * 根据分类来找菜品数据
     * @param categoryId
     * @param status 菜品的状态
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(long categoryId , Integer status){
        List<DishDto> list = dishService.findByCategoryId(categoryId , status);
        return R.success(list);
    }

    /**
     * 批量删除&删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        return R.success("删除成功！");
    }

    /**
     * 售卖状态更新
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStop( @PathVariable Integer status, @RequestParam List<Long> ids){
        dishService.updateStop(ids,status);
            return R.success("更新状态成功！");

    }

}
